//
//  TMCourseTypeController.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/8/24.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMCourseTypeController.h"
#import "TMCourseTypeCell.h"
#import "TMCourseTypeModel.h"
@interface TMCourseTypeController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *listCollection;
@property (nonatomic, assign) CGSize itemSize;
@end

@implementation TMCourseTypeController

-(void)setListCollection:(UICollectionView *)listCollection{
    _listCollection = listCollection;
    _listCollection.delegate = self;
    _listCollection.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 5.;
    flowLayout.minimumInteritemSpacing = 0.;
    _itemSize = CGSizeMake(kWindowW/2-17, 44 * kWindowW /375);
    _listCollection.collectionViewLayout = flowLayout;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程类别";
        
}
-(void)setCourseList:(NSMutableArray *)courseList{
    _courseList = courseList;
    [self.listCollection reloadData];
}
#pragma delegate datas

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return self.itemSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(7, 12, 0, 12);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.courseList.count > 0) {
         return self.courseList.count +1;
    }else{
         return 0;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TMCourseTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMCourseTypeCell" forIndexPath:indexPath];
    if (indexPath.item == 0) {
        cell.typeLabel.text = @"全部";
    }else{
    cell.model = self.courseList[indexPath.item-1];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CHOOSECOURSETYPENOTIFICATION" object:nil userInfo:@{@"tyid":@(10000),@"name":@"全部"}];
    }else{
    TMCourseTypeModel *model = self.courseList[indexPath.item-1];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHOOSECOURSETYPENOTIFICATION" object:nil userInfo:@{@"tyid":@(model.typesid),@"name":model.name}];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
